﻿/**
 * 提示信息模块
 */
(function(E){
var Consts = E.consts, timeids=[];


E.mod("Message", {
	show: function( msg, type, duration ){
		type = (type == 'error') ? 'error' : 'notice';
		MainMessage.show( msg, type, duration );
	},
	
	showError: function( msg, duration ){
		this.show(msg, 'error', duration);
	},
	
	showNotice: function( msg, duration ){
		this.show(msg, 'notice', duration);
	},
	
	change: function( msg ){
		msg && MainMessage.message && MainMessage.message.html(msg);
	},
	
	hide: function(){
		MainMessage.hide();
	}
});

E.Message = E.mod('Message');


var MainMessage = {
	
	message: null, 
	
	show: function( msg, type, duration ){
		var self=this,
			Toolbar = E.mod('Toolbar'),
			color = (type == 'error') ? 'red' : 'green',
			message = Toolbar.getMessage(),
			option = E.get('option'),
			speed = option.messageSpeed,
			height = Toolbar.get().height(),
			top = height + 1;
			
		duration = parseInt(duration, 10);
		if( isNaN(duration) ){
			duration = option.messageDuration;
		}else{
			duration = duration || option.messageDuration;
		}
		
		self.message = message;
		message.html(msg).css({color:color, top:top}).fadeIn(speed);
		
		clearTimeout(timeids[E.eid]);
		timeids[E.eid] = setTimeout(function(){
			self.hide();
		}, duration);
	},
	
	hide: function( speed ){
		var self = this, option = E.get('option');
		
		speed = speed ? parseInt(speed, 10) : 0;
		if( isNaN(speed) ){
			speed = option.messageSpeed;
		}else{
			speed = speed || option.messageSpeed;
		}
		
		self.message.fadeOut(speed);
	}
}

})(jQEditor);